/**
 * @Author: leibidong
 * @Description:根据一级类目名称查询商品信息
 * @File:  queryProductByName
 * @Version: 1.0.0
 * @Date: 2021/7/5 11:45
 */
package handler

import (
	"fmt"
	"ginProject/biz/service"
	"ginProject/biz/utils"
	"ginProject/biz/vo"
	"github.com/gin-gonic/gin"
)

/**
 * @Author leibidong
 * @Description 根据firstName查询商品,如果firstName为空，就查询所有商品
 * @Date  2021/7/5 11:48
 **/
func SelectProductByName(c *gin.Context) {
	firstName := c.Query("firstName")
	answer := service.SelectProduct(firstName)
	if answer == nil {
		utils.ReturnFailureMessage(nil, c)
	} else {
		var r []vo.ReturnProduct
		for _, val := range answer {
			t := vo.ReturnProduct{Id: val.ID, Name: val.Name, Encoding: val.Encoding, Price: val.Price, AverageCost: val.AverageCost, PublishStatus: val.PublishStatus, AuditStatus: val.AuditStatus, ColorType: utils.GetColor(val.ColorType), RecommendDescript: val.RecommendDescript, Descript: val.Descript}
			r = append(r, t)

		}
		utils.ReturnOKMessage(r, c)
	}
}

/**
 * @Author leibidong
 * @Description 添加商品
 * @Date  2021/7/5 12:44
 **/
func AddProduct(c *gin.Context) {
	var pro1 vo.AddProductMessage
	c.ShouldBind(&pro1)
	fmt.Println("输入", pro1)
	//service.AddProduct(pro1)
	utils.ReturnOKMessage("ok", c)
}

/**
 * @Author leibidong
 * @Description 更新商品信息
 * @Date  2021/7/5 15:10
 **/
func UpdateProduct(c *gin.Context) {
	var up vo.UpdateProductMessage
	c.ShouldBind(&up)
	status := service.UpdateProductMessage(up)
	if status == 0 {
		utils.ReturnFailureMessage(nil, c)
	} else {
		utils.ReturnOKMessage("ok", c)
	}
}

/**
 * @Author leibidong
 * @Description 删除产品信息
 * @Date  2021/7/11 17:36
 **/
func DeleteProduct(c *gin.Context) {
	id := c.Query("id")
	status := service.DeleteProduct(utils.StringToInt(id))
	if status == 0 {
		utils.ReturnFailureMessage(nil, c)
	} else {
		utils.ReturnOKMessage("ok", c)
	}
}
